/******************************************************************************
@file:  TransportHomeActivity.java
@author Ramesh Anandhan

DESCRIPTION
-----------------------------------------------------------------------------
Copyright (c) 2011, GEEKWAVE SOLUTIONS INC.

All rights reserved.
 ******************************************************************************/
package com.smartc.ui;

//import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.smartc.connection.HttpConnection;
import com.smartc.connection.ParsedLoginData; //import com.smartc.ui.BusRegister.ResponseHandler;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
//import android.view.View;
import android.widget.TextView;

public class TransportHomeActivity extends Activity {

	protected static final String TAG = "TransportHomeActivity";
	private TextView mService = null;
	private TextView mRoute = null;
	private TextView mName = null;
	private TextView mLocation = null;
	
	private HttpConnection mRegConnection;
	private ResponseHandler mResponse = new ResponseHandler();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.transport_home_activity);

		mService = (TextView) findViewById(R.id.transport_hm_service);
		mRoute = (TextView) findViewById(R.id.transport_hm_route);
		mName = (TextView) findViewById(R.id.transport_hm_name);
		mLocation = (TextView) findViewById(R.id.transport_hm_location);
	}

	@Override
	protected void onStart() {
		super.onStart();
		/*
		 * String domain = getResources().getString(R.string.domain); String
		 * reg_query = getResources().getString(R.string.transport_home_url);
		 * String reg_url = domain+reg_query; if(AccountLogin.DEBUG)
		 * Log.i(TAG,"onStart city_url:"+reg_url); mRegConnection = new
		 * HttpConnection(); mRegConnection.setUrl(reg_url);
		 * mRegConnection.clearParam(); //ParsedLoginData.mIdUser = "1";//TO_DO
		 * mRegConnection.AddParam("idUser", ParsedLoginData.mIdUser);
		 * mRegConnection.AddHandler(mResponse); mRegConnection.Execute();
		 */
	}

	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		super.onWindowFocusChanged(hasFocus);
		if (AccountLogin.DEBUG)
			Log.i(TAG, "onWindowFocusChanged() hasFocus:" + hasFocus);
		if (hasFocus) {
			String domain = getResources().getString(R.string.domain);
			String reg_query = getResources().getString(
					R.string.transport_home_url);
			String reg_url = domain + reg_query;
			if (AccountLogin.DEBUG)
				Log.i(TAG, "onStart city_url:" + reg_url);
			mRegConnection = new HttpConnection();
			mRegConnection.setUrl(reg_url);
			mRegConnection.clearParam();
			// ParsedLoginData.mIdUser = "1";//TO_DO
			mRegConnection.AddParam("idUser", ParsedLoginData.mIdUser);
			mRegConnection.AddHandler(mResponse);
			mRegConnection.Execute();
		}
	}

	public class ResponseHandler extends DefaultHandler {

		//private boolean mRegister = false;
		private boolean mService = false;
		private boolean mRoute = false;
		private boolean mName = false;
		private boolean nmBoardingLocation = false;
		//private boolean mMessage = false;
		private boolean mStatus = false;

		public String _mService;
		public String _mRoute;
		public String _mName;
		public String _mStatus;
		public String _nmBoardingLocation;
		@Override
		public void characters(char[] ch, int start, int length)
				throws SAXException {
			if (this.mService) {
				_mService = new String(ch, start, length);
			} else if (this.mRoute) {
				_mRoute = new String(ch, start, length);
			} else if (this.mName) {
				_mName = new String(ch, start, length);
			} else if (this.mStatus) {
				_mStatus = new String(ch, start, length);
			}else if (this.nmBoardingLocation) {
				_nmBoardingLocation = new String(ch, start, length);
			}
		}

		@Override
		public void endElement(String uri, String localName, String qName)
				throws SAXException {
			if (localName.equals("TransportRegister")) {
				//this.mRegister = false;
				Message myMessage = new Message();
				myMessage.obj = "REGISTERED";
				handler.sendMessage(myMessage);
			} else if (localName.equals("nmTransportService")) {
				this.mService = false;
			} else if (localName.equals("nmRoute")) {
				this.mRoute = false;
			} else if (localName.equals("nmFullname")) {
				this.mName = false;
			} else if (localName.equals("Messages")) {
				//this.mMessage = false;
				Message myMessage = new Message();
				myMessage.obj = "ERROR";
				handler.sendMessage(myMessage);
			} else if (localName.equals("status")) {
				this.mStatus = false;
			}else if(localName.equals("nmBoardingLocation")){
				this.nmBoardingLocation = false;
			}
		}

		@Override
		public void startElement(String uri, String localName, String qName,
				Attributes attributes) throws SAXException {
			if (localName.equals("TransportRegister")) {
				//this.mRegister = true;
			} else if (localName.equals("nmTransportService")) {
				this.mService = true;
			} else if (localName.equals("nmRoute")) {
				this.mRoute = true;
			} else if (localName.equals("nmFullname")) {
				this.mName = true;
			} else if (localName.equals("Messages")) {
				//this.mMessage = true;
			} else if (localName.equals("status")) {
				this.mStatus = true;
			}else if(localName.equals("nmBoardingLocation")){
				this.nmBoardingLocation = true;
			}
		}
	}

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if (AccountLogin.DEBUG)
				Log.i(TAG, "handleMessage msg:" + (String) msg.obj);
			String loginmsg = (String) msg.obj;
			if (loginmsg.equals("REGISTERED")) {
				mService.setText(mResponse._mService);
				mRoute.setText(mResponse._mRoute);
				mName.setText(mResponse._mName);
				mLocation.setText(mResponse._nmBoardingLocation);
			} else if (loginmsg.equals("ERROR")) {
				mService.setText("");
				mRoute.setText("");
				mName.setText("");
				mLocation.setText("");
			}
		}
	};

}
